<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局显示头部公司信息 -->
        <dept-tree :node-data="company" :is-root="true" @addDepartment="addDepartment" />
        <hr>
        <!-- 下面显示部门的树形组件 -->
        <el-tree
          :data="depts"
          :props="{label: 'name'}"
          default-expand-all
          :expand-on-click-node="false"
        >
          <!-- data 是 element tree 组件遍历数据后渲染时返回的数据
          就是当前节点的数据对象, 这里指的是部门数据对象 -->
          <template #default="{data}">
            <dept-tree
              :node-data="data"
              @addDepartment="addDepartment"
              @delDepartment="getDepartments"
              @ediDepartment="ediDepartment"
            />
          </template>
        </el-tree>
      </el-card>
      <add-dept
        ref="addDept"
        :show-dialog.sync="showDialog"
        :node-data="nodeData"
        @addDepartment="getDepartments"
      />
    </div>
  </div>
</template>

<script>
import deptTree from './components/dept-tree.vue'
import { getDepartments } from '@/api/department'
import { listToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: { deptTree, AddDept },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      depts: [],
      showDialog: false,
      nodeData: {}
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const { depts } = await getDepartments()
      // // console.log('后端来源', depts)
      this.depts = listToTreeData(depts, '')
    // // console.log('转换结果', this.depts)
    },
    addDepartment(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    async ediDepartment(nodeData) {
      await this.$refs.addDept.getDepartmentDetail(nodeData.id)
      this.showDialog = true
      this.nodeData = nodeData
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
